import scrapy
from qunaer_scrapy.items import QunaerScrapyItem


class QunarSpider(scrapy.Spider):
    name = 'qunar'
    allowed_domains = ['piao.qunar.com']
    # 桂林推荐景区爬取
    start_urls = ['https://piao.qunar.com/ticket/list.htm?keyword=%E6%A1%82%E6%9E%97%C2%AEion=&from=mpl_search_suggest']

    def parse(self, response):
        sight_items = response.css('#search-list .sight_item')
        for sight_item in sight_items:
            item = QunaerScrapyItem()
            # area（区域）、sight（景点）、level（等级）、price（价格）
            item['area'] = sight_item.css('::attr(data-districts)').extract_first()
            item['sight'] = sight_item.css('::attr(data-sight-name)').extract_first()
            item['level'] = sight_item.css('.level::text').extract_first()
            item['price'] = sight_item.css('.sight_item_price em::text').extract_first()
            yield item
        # 翻页
        next_url = response.css('.next::attr(href)').extract_first()
        if next_url:
            next_url = "https://piao.qunar.com" + next_url
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )
